﻿using ATMClientWinForm.ATMService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ATMClientWinForm
{
    public partial class GetCa : Form
    {
        ATMService.ATMServiceSoapClient client;
        Account acc;
       
        public GetCa(Account acc)
        {
            InitializeComponent();
            client = new ATMService.ATMServiceSoapClient();
            this.acc = acc;
            loadBalance();
           
        }

        private string loadBalance()
        {
            Account account = client.GetAccount(acc.Ip);
            this.acc = account;
            lblTotalBalance.Text = acc.Balance.ToString();
            return lblTotalBalance.Text;
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            ManageATM mn = new ManageATM(this.acc);
            if (txtMoney.Text.Trim() != "")
            {
                int money = Convert.ToInt32(txtMoney.Text.Trim());
                WithdrawMoney(money, this.acc.Ip);
               
            }
            else
                MessageBox.Show("Please enter Money been withdraw");

        }

        

        //void withdraw(int money, string ip) {
        //    if (client.ExceptMoney(money, ip)) {
        //        int balance = (int)(this.acc.Balance - money);
        //        string description = txtContent.Text;
        //        Transaction sending = new Transaction
        //        {
        //            IpFrom = ip,
        //            IpTo="",
        //            Type = "Withdrawal",
        //            Amount = money,
        //            Balance = balance,
        //            Description = description,
        //            time = DateTime.Now
        //        };
        //        client.SendingTransfer(sending);
        //    }
        //}

        void WithdrawMoney(int money, string Ip)
        {
            if (money < this.acc.Balance)
            {
                if (this.acc.Balance - money > 100000)
                {
                    if (money > 100000 && money < 5000000)
                    {
                        bool a = client.ExceptMoney(money, Ip);
                        if (a == true)
                            loadBalance();
                        txtMoney.Text = "";
                       
                        Transaction sending = new Transaction
                        {
                            IpFrom = this.acc.Ip,
                            Type = "Withdrawal",
                            Amount = money,
                            Balance = Convert.ToDecimal(loadBalance()),
                            Description = "",
                            time = DateTime.Now
                        };
                        bool SendingTransfer = client.SendingTransfer(sending);
                        MessageBox.Show("Withdraw Money Success..!");

                    }
                    else
                        MessageBox.Show("Money entered must be greater 100.000 and  less than 5.000.000 ");

                }
                else
                    MessageBox.Show("Total balance must be greater 100.000 ");

            }
            else
                MessageBox.Show("Money enter  not exceeding Total Balance");



        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {

                if (e.Handled = !char.IsNumber(e.KeyChar))
                {
                    MessageBox.Show("Must enter number");
                }

            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            ManageATM ad = new ManageATM(this.acc);
            ad.Show();
            this.Close();
        }
    }
}
